class PartConfigurationsController < ApplicationController
  # GET /part_configurations
  # GET /part_configurations.xml
  def index
    @part_configurations = PartConfiguration.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @part_configurations }
    end
  end

  # GET /part_configurations/1
  # GET /part_configurations/1.xml
  def show
    @part_configuration = PartConfiguration.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @part_configuration }
    end
  end

  # GET /part_configurations/new
  # GET /part_configurations/new.xml
  def new
    @part_configuration = PartConfiguration.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @part_configuration }
    end
  end

  # GET /part_configurations/1/edit
  def edit
    @part_configuration = PartConfiguration.find(params[:id])
  end

  # POST /part_configurations
  # POST /part_configurations.xml
  def create
    @part_configuration = PartConfiguration.new(params[:part_configuration])
    @part = Part.find_by_name(@part_configuration.name.downcase)
    @part_configuration.part = @part
    if !@part.nil?
      @part_configuration.parts_group = @part.parts_group
    end
    
    @cart = @part_configuration.cart
    @part_configuration.save
    respond_to do |format|
      if @part_configuration.save
        format.html { render @cart }
        format.js
        format.xml  { render :xml => @part_configuration, :status => :created, :location => @part_configuration }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @part_configuration.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /part_configurations/1
  # PUT /part_configurations/1.xml
  def update
    @part_configuration = PartConfiguration.find(params[:id])

    respond_to do |format|
      if @part_configuration.update_attributes(params[:part_configuration])
        format.html { redirect_to(@part_configuration, :notice => 'Part configuration was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @part_configuration.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /part_configurations/1
  # DELETE /part_configurations/1.xml
  def destroy
    @part_configuration = PartConfiguration.find(params[:id])
    @cart = @part_configuration.cart
    @part_configuration.destroy

    respond_to do |format|
      format.html { redirect_to(root_url) }
      format.js
    end
  end
end
